import { CACHE_KEY, useCache } from '@/hooks/basic/useCache'
import { i18n } from './index'

const { wsCache } = useCache()

// 设置语言
export const setHtmlPageLang = (locale: LocaleType) => {
  document.querySelector('html')?.setAttribute('lang', locale)
}

// 扩展函数 - 设置语言
export const setI18nLanguage = async (locale: LocaleType) => {
  wsCache.set(CACHE_KEY.LANG, locale)
  
  const globalI18n = i18n.global
  const langModule = await import(`../../locales/${locale}.ts`)

  globalI18n.setLocaleMessage(locale, langModule.default)

  if (i18n.mode === 'legacy') {
    i18n.global.locale = locale
  } else {
    ;(i18n.global.locale as any).value = locale
  }
  setHtmlPageLang(locale)
}

